<?php

namespace app\admin\controller\integral;

use app\common\controller\Backend;
use think\Session;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Teachertel extends Backend
{
    
    /**
     * Tel模型对象
     * @var \app\admin\model\teacher\Tel
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\teacher\Tel;

    }

    /**
     * 查看
     */
    public function index($ids=null,$userid=null)
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        $this->assign('userid',$userid);
        $this->assign('ids',$ids);
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add($ids=null)
    {
        $admin = Session::get('admin');
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params['userid'] = $admin['userid'];
                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                        $this->model->validate($validate);
                    }
                    $result = $this->model->allowField(true)->save($params);
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($this->model->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("week_id", $ids);
        return $this->view->fetch();
    }

}
